package com.gs.wzweather.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/11 0011
 */
public class HttpUtils {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public static ResponseEntity<String> buildStringResponse(String str, HttpStatus status) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Type", "text/plain;charset=utf-8;");
        return new ResponseEntity<>(str, responseHeaders, status);
    }

    public static String request(String url) {
        StringBuffer sbf = new StringBuffer();
        String result = null;
        BufferedReader reader = null;
        try {
            URL mUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            System.out.print("request->" + e.getMessage());
        } finally {
            return result;
        }
    }

    public static String FileResponse(File file, HttpServletResponse response) {
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buf = new byte[100 * 1024];
            int len;
            while ((len = bis.read(buf)) != -1){
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }
            fis.close();
            bis.close();
            outputStream.close();
            return null;

        } catch (IOException e) {
            e.printStackTrace();
            return "failed";

        }
    }

    public static String FileDown(File file, HttpServletResponse response,String fileName) {
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            response.setCharacterEncoding("UTF-8");
            response.reset();
            response.setContentType("text/x-plain");// 定义输出类型
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );
            //new String( fileName.getBytes("gb2312"), "ISO8859-1" )
            //java.net.URLEncoder.encode(fileName, "UTF-8")
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buf = new byte[100 * 1024];
            int len;
            while ((len = bis.read(buf)) != -1){
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }
            fis.close();
            bis.close();
            outputStream.close();
            return null;

        } catch (IOException e) {
            e.printStackTrace();
            return "failed";

        }
    }

    public static String FileResponse(InputStream is, HttpServletResponse response) {
        try {
            BufferedInputStream bis = new BufferedInputStream(is);
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buf = new byte[100 * 1024];
            int len;
            while ((len = bis.read(buf)) != -1){
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }
            is.close();
            bis.close();
            outputStream.close();
            return null;

        } catch (IOException e) {
            e.printStackTrace();
            return "failed";

        }
    }

}
